package view;

import javax.swing.JDialog;

import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import controller.ContactsControllerListener;
import controller.JTextFieldListener;
import po.*;

/**
 * 添加学生信息对话框
 * @author 27261
 *
 */
public class StudentDialog extends JDialog {
	private static final long serialVersionUID = 1L;
	private ContactsControllerListener contactsControllerListener;
	private JTextField studentIdTextField = new JTextField();
	private JTextField nameTextField = new JTextField();
	private JTextField roomNameTextField = new JTextField();
	private JTextField dateTextField = new JTextField();
	private JTextField emailTextField = new JTextField();
	private JTextField nativePlaceTextField = new JTextField();
	private JTextField addressTextField = new JTextField();
	private JTextField phoneNumTextField = new JTextField();
	private JButton confirmButton = new JButton();
	private JPanel studentAddPanel = new JPanel();
	private Student student;
	public StudentDialog(ContactsFrame owner,String title,Student student) {
		this.contactsControllerListener = owner.getControllerListener();  //让StudentFrame和Dialog共用同一个controller对象
		this.contactsControllerListener.setStudentAddDialog(this);
		this.setTitle(title);
		this.setResizable(false);
		this.student = student;
		this.add(getStudentAddPanel());
		this.setModal(true);
		this.setSize(250, 320);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.validate();
	}
	public StudentDialog(OverviewDialog owner,String title,Student student) {
		this.contactsControllerListener = owner.getControllerListener();  //让StudentFrame和Dialog共用同一个controller对象
		this.contactsControllerListener.setStudentAddDialog(this);
		this.setTitle(title);
		this.setResizable(false);
		this.student = student;
		this.add(getStudentAddPanel());
		this.setModal(true);
		this.setSize(250, 320);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.validate();
	}
	public void closeCurrentDialog() {
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}
	/**
	 * 添加学生信息表单面板
	 * @return
	 */
	private JPanel getStudentAddPanel() {
		studentAddPanel = new JPanel();
		if("添加学生信息".equals(this.getTitle())) {
			studentIdTextField = new JTextField(12);
			nameTextField = new JTextField(12);
			roomNameTextField = new JTextField(12);
			roomNameTextField.addFocusListener(new JTextFieldListener(roomNameTextField, "例如：泰山区1-111"));
			dateTextField = new JTextField(12);
			dateTextField.addFocusListener(new JTextFieldListener(dateTextField, "例如：2000-01-01"));
			emailTextField = new JTextField(12);
			nativePlaceTextField = new JTextField(12);
			nativePlaceTextField.addFocusListener(new JTextFieldListener(nativePlaceTextField, "例如：广东汕头"));
			addressTextField = new JTextField(12);
			addressTextField.addFocusListener(new JTextFieldListener(addressTextField, "例如：广东汕头"));
			phoneNumTextField = new JTextField(12);
			confirmButton = new JButton("确定添加");
			confirmButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
			confirmButton.setPreferredSize(new Dimension(100, 30));
			confirmButton.setActionCommand("studentAddPost"); //添加学生信息
		}else {
			studentIdTextField = new JTextField(String.valueOf(student.getStudentId()),12);
			nameTextField = new JTextField(student.getName(),12);
			roomNameTextField = new JTextField(String.valueOf(student.getRoom().getRoom_name()),12);
			dateTextField = new JTextField(String.valueOf(student.getBirthday()),12);
			emailTextField = new JTextField(student.getEmail(),12);
			nativePlaceTextField = new JTextField(student.getNative_place(),12);
			addressTextField = new JTextField(student.getAddress(),12);
			phoneNumTextField = new JTextField(String.valueOf(student.getPhoneNumber()),12);
			confirmButton = new JButton("确定修改");
			confirmButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
			confirmButton.setPreferredSize(new Dimension(100, 30));
			confirmButton.setActionCommand("updatePost"); //修改学生信息
		}
		confirmButton.addActionListener(contactsControllerListener);
		JLabel a = new JLabel("姓          名:");
		JLabel b = new JLabel("学          号:");
		JLabel c = new JLabel("大 学 宿 舍:");
		JLabel d = new JLabel("生          日:");
		JLabel e = new JLabel("手 机 号 码:");
		JLabel f = new JLabel("邮           箱:");
		JLabel g = new JLabel("目前所在地:");
		JLabel h = new JLabel("出    生    地:");
		a.setFont(new Font("微软雅黑",Font.PLAIN,14));
		b.setFont(new Font("微软雅黑",Font.PLAIN,14));
		c.setFont(new Font("微软雅黑",Font.PLAIN,14));
		d.setFont(new Font("微软雅黑",Font.PLAIN,14));
		e.setFont(new Font("微软雅黑",Font.PLAIN,14));
		f.setFont(new Font("微软雅黑",Font.PLAIN,14));
		g.setFont(new Font("微软雅黑",Font.PLAIN,14));
		h.setFont(new Font("微软雅黑",Font.PLAIN,14));
		studentAddPanel.add(a);
		studentAddPanel.add(nameTextField);
		studentAddPanel.add(b);
		studentAddPanel.add(studentIdTextField);
		studentAddPanel.add(c);
		studentAddPanel.add(roomNameTextField);
		studentAddPanel.add(d);
		studentAddPanel.add(dateTextField);
		studentAddPanel.add(e);
		studentAddPanel.add(phoneNumTextField);
		studentAddPanel.add(f);
		studentAddPanel.add(emailTextField);
		studentAddPanel.add(g);
		studentAddPanel.add(addressTextField);
		studentAddPanel.add(h);
		studentAddPanel.add(nativePlaceTextField);
		studentAddPanel.add(confirmButton);
		return studentAddPanel;
	}
	public JTextField getStudentIdTextField() {
		return studentIdTextField;
	}
	public JTextField getNameTextField() {
		return nameTextField;
	}
	public JTextField getRoomNameTextField() {
		return roomNameTextField;
	}
	public JTextField getDateTextField() {
		return dateTextField;
	}
	public JTextField getEmailTextField() {
		return emailTextField;
	}
	public JTextField getNativePlaceTextField() {
		return nativePlaceTextField;
	}
	public JTextField getPhoneNumTextField() {
		return phoneNumTextField;
	}
	public JTextField getAddressTextField() {
		return addressTextField;
	}
	public JButton getConfirmButton() {
		return confirmButton;
	}
	public StudentDialog() {
		super();
	}
	
}